package com.xp.basic.heima.nio.netty.simpledemo.chat.client;

import com.xp.basic.heima.nio.netty.simpledemo.chat.handler.RpcResponseHandler;
import com.xp.basic.heima.nio.netty.simpledemo.chat.message.RpcRequestMessage;
import com.xp.basic.heima.nio.netty.simpledemo.chat.protocol.MessageCodec;
import com.xp.basic.heima.nio.netty.simpledemo.chat.protocol.ProcotolFrameDecoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.net.InetSocketAddress;

public class RpcClientDemo1 {
    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup c1 = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        final LoggingHandler LOGGING_HANDLER = new LoggingHandler();
        final MessageCodec MESSAGE_CODEC = new MessageCodec();
        final RpcResponseHandler RPC_HANDLER = new RpcResponseHandler();

        Channel channel = bootstrap.group(c1)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ProcotolFrameDecoder());
                        ch.pipeline().addLast(LOGGING_HANDLER);
                        ch.pipeline().addLast(MESSAGE_CODEC);
                        ch.pipeline().addLast(RPC_HANDLER);
                    }

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("激活了，准备发送rpc消息了");
                        //通道激活的时候就开始发送消息
                        RpcRequestMessage rpcRequestMessage = new RpcRequestMessage(1,
                                "com.xp.basic.heima.nio.netty.simpledemo.chat.service.HelloServiceImpl",
                                "sayHello",
                                Void.class, new Class[]{String.class}, new Object[]{"李四"});
                        ctx.writeAndFlush(rpcRequestMessage);
                    }
                }).connect(new InetSocketAddress(8080))
                .sync().channel();
        //通道激活的时候就开始发送消息
        RpcRequestMessage rpcRequestMessage = new RpcRequestMessage(1,
                "com.xp.basic.heima.nio.netty.simpledemo.chat.service.HelloServiceImpl",
                "sayHello",
                Void.class, new Class[]{String.class}, new Object[]{"李四"});
        channel.writeAndFlush(rpcRequestMessage)
                .addListener(l->{
                    if (l.isSuccess()) {
                        System.out.println("发送rpc成功");
                    }else{
                        Throwable cause = l.cause();
                        System.out.println("cause.getMessage() = " + cause.getMessage());
                    }
                });

    }
}
